//
//  TWSVisitorView.m
//  TWSWeibo
//
//  Created by Twisal on 2017/1/4.
//  Copyright © 2017年 Twisal. All rights reserved.
//

#import "TWSVisitorView.h"

@interface TWSVisitorView ()
@property (nonatomic, strong)UIImageView *iconView;
@property (nonatomic, strong)UIImageView *maskIconView;
@property (nonatomic, strong)UIImageView *houseView;
@property (nonatomic, strong)UILabel *tipLabel;

@end

@implementation TWSVisitorView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}


/**
 页面布局
 */
- (void)setupUI {
    self.backgroundColor = [UIColor cz_colorWithHex:0xEDEDED];
    [self addSubview:self.iconView];
    [self addSubview:self.maskIconView];
    [self addSubview:self.houseView];
    [self addSubview:self.tipLabel];
    [self addSubview:self.registerBtn];
    [self addSubview:self.loginBtn];
    
    
    // 设置tipLabel文字居中对齐
    _tipLabel.textAlignment = NSTextAlignmentCenter;
    
    // 取消autoresizing
    for (UIView *v in self.subviews) {
        v.translatesAutoresizingMaskIntoConstraints = NO;
    }
    CGFloat margin = 20.0;
    // iconView
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:-60]];
    
    // houseView
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.houseView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.houseView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
    
    // tipLabel
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tipLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tipLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:margin]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tipLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:236]];
    
    // registerBtn
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerBtn attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.tipLabel attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.tipLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:margin]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100]];
    
    // loginBtn
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginBtn attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.tipLabel attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.tipLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:margin]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100]];
    
    // maskIconView
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.maskIconView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.maskIconView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.maskIconView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.maskIconView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.registerBtn attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
}


/**
 旋转动画
 */
- (void)startAnimation {
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    anim.toValue = @(2 * M_PI);
    anim.repeatCount = MAXFLOAT;
    anim.duration = 15;
    anim.removedOnCompletion = false;
    [self.iconView.layer addAnimation:anim forKey:nil];
    
}

/**
 重写访客视图参数字典的setter方法

 @param visitorInfo 传入的参数字典
 */
- (void)setVisitorInfo:(NSDictionary *)visitorInfo {
    NSString *visitorImageName = visitorInfo[@"visitorImageName"];
    NSString *visitorMessage = visitorInfo[@"visitorMessage"];
    self.tipLabel.text = visitorMessage;
    // @""表示首页, 默认会添加
    if ([visitorImageName isEqualToString:@""]) {
        [self startAnimation];
        return;
    }
    
    self.iconView.image = [UIImage imageNamed:visitorImageName];
    self.maskIconView.hidden = YES;
    self.houseView.hidden = YES;
}

#pragma mark - 懒加载
- (UIImageView *)iconView {
    if (!_iconView) {
        _iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
    }
    return _iconView;
}

- (UIImageView *)houseView {
    if (!_houseView) {
        _houseView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];
    }
    return _houseView;
}

- (UIImageView *)maskIconView {
    if (!_maskIconView) {
        _maskIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
    }
    return _maskIconView;
}

- (UILabel *)tipLabel {
    if (!_tipLabel) {
        _tipLabel = [UILabel cz_labelWithText:@"关注一些人,回这里看看有什么惊喜" fontSize:14 color:[UIColor darkGrayColor]];
    }
    return _tipLabel;
}

- (UIButton *)registerBtn {
    if (!_registerBtn) {
        _registerBtn = [UIButton cz_textButton:@"注册" fontSize:16 normalColor:[UIColor orangeColor] highlightedColor:[UIColor darkGrayColor] backgroundImageName:@"common_button_big_white_disable"];
        
    }
    return _registerBtn;
}

- (UIButton *)loginBtn {
    if (!_loginBtn) {
        _loginBtn = [UIButton cz_textButton:@"登录" fontSize:16 normalColor:[UIColor darkGrayColor] highlightedColor:[UIColor orangeColor] backgroundImageName:@"common_button_big_white_disable"];
    }
    return _loginBtn;
}

@end
